<?php

require("../qcubed.inc.php");
class UserEdit extends QForm {

    protected $mctUser;

    // Controls for User's Data Fields
    protected $lblId;
    protected $txtUserName;
    protected $txtPassword1;
    protected $txtPassword2;
    protected $lstPerson;
    protected $lstUserTypeObject;
    protected $chkActive;
    protected $lstPrimaryRole;
    protected $lblLastLogin;


    // Other ListBoxes (if applicable) via Unique ReverseReferences and ManyToMany References
    protected $lstRolesAsEffective;

    // Other Controls
    protected $btnSave;
    protected $btnDelete;
    protected $btnCancel;

    protected $AuthSys;

    // Create QForm Event Handlers as Needed

//		protected function Form_Exit() {}
//		protected function Form_Load() {}
//		protected function Form_PreRender() {}

    public function Role_Change($strParameter) {
        $objRoleArray = Role::QueryArray(
                QQ::Like(QQN::Role()->Name, $strParameter . '%'),
                QQ::Clause(QQ::OrderBy(QQN::Role()->Name))
        );
        $result = array();
        foreach($objRoleArray as $objMember) {
            $result[] = $objMember->Name . "|" . $objMember->Id ;
        }
        return $result;
    }
    
    protected function Form_Run() {
        // Security check for ALLOW_REMOTE_ADMIN
        $this->AuthSys = new Perm();
        $this->AuthSys->IsAdmin(true);
    }

    public function lstPerson_Select($mixSelected) {

        $strParamArray = explode('|', $mixSelected);
        if (count($strParamArray) == 2) {
            $x = (int)$strParamArray[1];
            QApplication::DisplayAlert("X=" . $x);
        }
    }

    public function lstPerson_Change($strParameter) {
        $objMemberArray = Person::QueryArray(
                QQ::OrCondition(
                QQ::Like(QQN::Person()->FirstName,$strParameter . '%' ),
                QQ::Like(QQN::Person()->LastName, $strParameter . '%')
                ),
                QQ::Clause(QQ::OrderBy(QQN::Person()->FirstName))
        );
        $result = array();
        foreach($objMemberArray as $objMember) {
            $result[] = $objMember->FirstName . " " . $objMember->LastName . "|" . $objMember->Id . chr(10) ;
        }

        return $result;
    }
    protected function Form_Create() {
        // Use the CreateFromPathInfo shortcut (this can also be done manually using the UserMetaControl constructor)
        // MAKE SURE we specify "$this" as the MetaControl's (and thus all subsequent controls') parent
        $this->mctUser = UserMetaControl::CreateFromPathInfo($this);
        $this->mctUser->blnAdmin = true;

        // Call MetaControl's methods to create qcontrols based on User's data fields
        $this->lblId = $this->mctUser->lblId_Create();
        $this->txtUserName = $this->mctUser->txtUserName_Create();

        $this->txtPassword1 = $this->mctUser->txtPassword1_create();
        $this->txtPassword2 = $this->mctUser->txtPassword2_create();

        $this->lstPerson = $this->mctUser->lstPerson_Create();
        $this->lstPerson->Name="Person";
        $this->lstUserTypeObject = $this->mctUser->lstUserTypeObject_Create();

        $this->chkActive = $this->mctUser->chkActive_Create();
        $this->lstPrimaryRole = $this->mctUser->lstPrimaryRole_Create();
        $this->lblLastLogin = $this->mctUser->lblLastLogin_Create();

        $this->lstRolesAsEffective = $this->mctUser->lstRolesAsEffective_Create();

        // Create Buttons and Actions on this Form
        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
        $this->btnSave->CausesValidation = true;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));

        $this->btnDelete = new QButton($this);
        $this->btnDelete->Text = QApplication::Translate('Delete');
        $this->btnDelete->AddAction(new QClickEvent(), new QConfirmAction(QApplication::Translate('Are you SURE you want to DELETE this') . ' ' . QApplication::Translate('User') . '?'));
        $this->btnDelete->AddAction(new QClickEvent(), new QAjaxAction('btnDelete_Click'));
        $this->btnDelete->Visible = $this->mctUser->EditMode;
    }

    /**
     * This Form_Validate event handler allows you to specify any custom Form Validation rules.
     * It will also Blink() on all invalid controls, as well as Focus() on the top-most invalid control.
     */
    protected function Form_Validate() {
        // By default, we report that Custom Validations passed
        $blnToReturn = true;

        // Custom Validation Rules
        // TODO: Be sure to set $blnToReturn to false if any custom validation fails!
        // Check for records that may violate Unique Clauses
        if (($objUser = User::LoadByUserName($this->txtUserName->Text)) && ($objUser->Id != $this->mctUser->User->Id )) {
            $blnToReturn = false;
            $this->txtUserName->Warning = QApplication::Translate("Already in Use");
        }

        //Check must be same password 1 and 2
        if ( ( strlen($this->txtPassword1->Text) > 0) || ( strlen($this->txtPassword2->Text) > 0) ) {
            if ($this->txtPassword1->Text <> $this->txtPassword2->Text) {
                $blnToReturn=false;
                $this->txtPassword1->Warning = QApplication::Translate("Password does not match");
                $this->txtPassword2->Warning = QApplication::Translate("Password does not match");
            }
        }




        $blnFocused = false;
        foreach ($this->GetErrorControls() as $objControl) {
            // Set Focus to the top-most invalid control
            if (!$blnFocused) {
                $objControl->Focus();
                $blnFocused = true;
            }

            // Blink on ALL invalid controls
            $objControl->Blink();
        }

        return $blnToReturn;
    }

    // Button Event Handlers


    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Save" processing to the UserMetaControl
        $this->mctUser->SaveUser();
        $this->RedirectToListPage();
    }

    protected function btnDelete_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Delete" processing to the UserMetaControl
        $this->mctUser->DeleteUser();
        $this->RedirectToListPage();
    }

    protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {

        $this->RedirectToListPage();
    }

    // Other Methods

    protected function RedirectToListPage() {
        QApplication::Redirect(__SECURITY__ . '/user_list.php');
    }
}
UserEdit::Run('UserEdit','tpl/user_edit.tpl.php');
?>
